package com.yycx.module.bus.provider.handler.haiying.custombalance;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.client.enums.DeptTypeEnum;
import com.yycx.module.bus.provider.handler.haiying.BusinessUtils;
import com.yycx.module.bus.provider.service.hy.BusBankStatementService;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysBankService;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 认款保存处理器
 *
 * @author zyf
 */
@Component
public class HyYfkPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Resource
    private BusinessUtils businessUtils;

    @Resource
    private SysCompanyService companyService;

    @Resource
    private BusCustomService customService;

    @Resource
    private BusSalemanService salemanService;

    @Resource
    private BusDeptService deptService;

    @Resource
    private SysBankService bankService;

    @Resource
    private BusBankStatementService bankStatementService;


    @Override
    public void prepare(CriteriaUpdate cu, EntityMap entityMap, BusPayment payment) {
        EntityMap params = cu.getRequestMap();
        payment.setCompanyId(OpenHelper.getCompanyId());
        initData(params, payment);
        SaveInterceptor.super.prepare(cu, params, payment);
    }

    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        EntityMap params = cs.getRequestMap();
        Long companyId = OpenHelper.getCompanyId();
        Long userId = OpenHelper.getUserId();
        payment.setOrganizationId(OpenHelper.getOrganizationId());
        payment.setCompanyId(companyId);
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setPaymentState(CommonConstants.INT_0);
        payment.setSurplusAmount(payment.getAmount());
        payment.setRenDate(DateUtils.getNowDate());
        payment.setCustomId(null);
        String paymentNo = businessUtils.getPaymentNo(companyId, userId);
        payment.setPaymentNo(paymentNo);
        initData(params, payment);
    }

    private void initData(EntityMap params, BusPayment payment) {
        String customCode = params.get("customCode");
        String amountType = payment.getAmountType();
        String name = AmountTypeEnum.getName(amountType);
        payment.setTitle(name + "收款单");
        Long companyId = payment.getCompanyId();
        //是否是提前认款
        payment.setAdvanceState(1);
        if (FlymeUtils.isNotEmpty(customCode)) {
            BusCustom custom = customService.selectByCustomCode(customCode, companyId);
            SysCompany company = companyService.getById(companyId);
            if (FlymeUtils.isEmpty(custom)) {
                //同步客商资料
                custom = customService.syncBusCustom(company.getBusinessScope(), customCode, companyId);
            }
            if (FlymeUtils.isNotEmpty(custom)) {
                payment.setCustomId(custom.getCustomId().toString());
            }
        }
        Long deptId = payment.getDeptId();
        String deptType = null;
        if (FlymeUtils.isNotEmpty(deptId)) {
            BusDept dept = deptService.getById(deptId);
            deptType = dept.getDeptType();
        }
        Long saleManId = payment.getSaleManId();
        //查询业务员
        BusSaleman saleMan = salemanService.getById(saleManId);
        if (FlymeUtils.isNotEmpty(saleMan)) {
            String businessArea = saleMan.getAreaShortName();
            if (deptType.equals(DeptTypeEnum.ZS.getCode())) {
                businessArea = saleMan.getSaleManBusinessNo();
            }
            payment.setBusinessArea(businessArea);
        }
        //收款银行ID
        Long bankId = payment.getBankId();
        if (FlymeUtils.isNotEmpty(bankId)) {
            SysBank bank = bankService.getById(bankId);
            payment.setBankName(bank.getBankName());
            payment.setBankNo(bank.getBankNo());
        }
    }


    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String bankStatementId = cs.getParams("bankStatementId");
        if (FlymeUtils.isNotEmpty(bankStatementId)) {
            bankStatementService.updateBankStatement(bankStatementId, entity.getPaymentId(), CommonConstants.INT_1);
        }
    }

    @Override
    public void complete(CriteriaUpdate cu, EntityMap params, BusPayment payment) {
        String bankStatementId = cu.getParams("bankStatementId");
        String oldBankStatementId = cu.getParams("oldBankStatementId");
        if (FlymeUtils.isNotEmpty(bankStatementId)) {
            bankStatementService.updateBankStatement(bankStatementId, payment.getPaymentId(), CommonConstants.INT_1);
        }
        if (FlymeUtils.isNotEmpty(oldBankStatementId) && !oldBankStatementId.equals(bankStatementId)) {
            bankStatementService.updateBankStatement(oldBankStatementId, payment.getPaymentId(), CommonConstants.INT_0);
        }
    }
}
